var api = require('../../utils/util.js').default;
var app = getApp();
Page({
  // 页面数据
  data: {
    is_defined: false,
    nick_name: null,
    school_num: null,
    show_name: null,
    show_school: null,
    show_define: null,
    confirmBoxVisible: false,
    pho: null,
    code: null,
    has_confirm: false,
  },
  // 预加载
  onLoad: function (options) {
    // 渲染数据
    this.setData({
      is_defined: app.globalData.is_defined,
      show_name: app.globalData.user_name,
      show_school: app.globalData.school_num ? app.globalData.school_num : '输入学号',
      show_define: app.globalData.is_defined ? "已认证" : "未认证",
    })
    // 初始化数据
    wx.setStorageSync('tempname', null);
    wx.setStorageSync('tempschoolnum', null);
  },

  // 返回右上角按钮触发事件
  onUnload: function () {
    if (wx.getStorageSync('tempname') || wx.getStorageSync('tempschoolnum')) {
      if (!this.data.has_confirm) {
        wx.showModal({
          title: '温馨提示',
          content: '确认保存编辑个人信息？',
          showCancel: true,
          cancelText: '取消',
          cancelColor: '#000000',
          confirmText: '确定',
          confirmColor: '#3CC51F',
          success: (result) => {
            if (result.confirm) {
              this.submitBasicInfo();
            }
          },
          fail: () => {},
          complete: () => {}
        });
      }
    }
  },

  // 输入框获取 
  getNickname: function (e) {
    wx.setStorageSync('tempname', e.detail.value);
  },
  getSchoolNum: function (e) {
    wx.setStorageSync('tempschoolnum', e.detail.value);
  },

  // 获取手机号响应事件
  getPhoneNumber(e) {
    var self = this
    wx.setStorageSync('iv', e.detail.iv);
    wx.setStorageSync('encryptedData', e.detail.encryptedData);
    // 获取登录通讯秘钥
    wx.login({
      success: function (res) {
        wx.setStorageSync('code', res.code);
        var data = {
          code: wx.getStorageSync('code'),
          iv: wx.getStorageSync('iv'),
          da: wx.getStorageSync('encryptedData')
        }
        api.getphone(data).then(res => {
          console.log(res.data)
          if (res.data.state == true) {
            app.globalData.user_pho = res.data.user_pho
            app.globalData.is_defined = res.data.state
            self.setData({
              is_defined: true,
              show_define: '已认证'
            })
          } else {
            wx.showToast({
              title: '认证繁忙,请重试',
              icon: 'loading',
              duration: 1000,
            });
          }
        })
      },
    })
  },

  // 提交姓名学号接口
  submitBasicInfo: function () {
    if (wx.getStorageSync('tempname') || wx.getStorageSync('tempschoolnum')) {
      var data = {
        openid: app.globalData.openid,
        name: wx.getStorageSync('tempname') ? wx.getStorageSync('tempname') : app.globalData.user_name,
        school_num: wx.getStorageSync('tempschoolnum') ? wx.getStorageSync('tempschoolnum') : app.globalData.school_num,
      }
      api.updateBasicInfo(data).then(res => {
        if (res.data.state) {
          app.globalData.user_name = res.data.user_name
          app.globalData.school_num = res.data.school_num
          this.setData({
            show_name: res.data.user_name,
            show_school: res.data.school_num,
            has_confirm: true,
          })
          wx.showToast({
            title: '修改成功',
            icon: 'success',
            duration: 800,
          });
        } else {
          wx.showToast({
            title: '服务繁忙',
            icon: 'loading',
            duration: 800,
          })
        }
        console.log(res.data)
      })
    }
  },
  
  // 骑手注册页面 
  gotoriderRegister: function () {
    if(app.globalData.is_rider=='0'){
      if(app.globalData.is_defined){
        wx.navigateTo({
          url: '../user_riderRegister/user_riderRegister'
        })
      }else{
        wx.showModal({
          title: '温馨提醒',
          content: '请先完成实名认证后再申请成为骑手',
          showCancel: false,
          confirmText: '确定',
          confirmColor: '#3CC51F',
        });
      }
    }else{
      wx.showToast({
        title: '已经申请',
        icon: 'loading',
        duration: 1000,
        mask: false,
      });
    }

  },

})